//index.js
var hprose = require('../../utils/hprose.js');
var co = hprose.co;
var regeneratorRuntime = require("../../utils/regenerator-runtime.js");
//var REQEST_URI = 'http://127.0.0.1'; 
var REQEST_URI = 'https://www.josyx.club'; 

//获取应用实例
const app = getApp()

Page({
  data: {
    paperId:1,
    questId:0,
    question:'',
    total:'',
    correct_point: 0,
    point:0,
    items: []
  },
  //事件处理函数
  bindViewTap: function (e) {
   
    var client = hprose.Client.create(REQEST_URI + "/Quiz_Exam/api", ["getQuestJsonAction"]);
    var that2 = this
    console.log(this.data.questId)
    client.getQuestJsonAction(
      JSON.stringify({
        'paperId': 1,
        'session': app.globalData.session,
        'exam': {
          'questId': this.data.questId, 'answer': e.currentTarget.dataset.id}
      })).then(function (result) {
        console.log(222)
        var row = JSON.parse(result);

        if (row.code == 201) {
          wx.redirectTo({
            url: '/pages/detail/detail'
          })
        /*  that2.setData({
            question: '您已经回答完全部问题,得分为' + row.point,
            items: []
          }) */
        } else if (row.code == 300) {
          that2.setData({
            question: row.message,
            items: []
          })
        } else if (row.code == 200) {
          that2.setData({
            questId: row.quest.id,
            question: row.quest.question,
            items: row.answers,
            total: row.total,
            current: row.current,
            correct_point: row.quest.correct_point
          })
        } else {
          console.log('system error');
          console.log(row);
        }
      });
    return;
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log(res.tempImagePath);    
      }
    })
    //  wx.navigateTo({
    //    url: '../logs/logs'
    //  })
  },
  onLoad: function () {
    var client = hprose.Client.create(REQEST_URI + "/Quiz_Exam/api", ["getQuestJsonAction"]);
    var that = this
    client.getQuestJsonAction(
      JSON.stringify({
        'paperId': 1,
        'session': app.globalData.session,
      })).then(function (result) {
        var row = JSON.parse(result);

        if (row.code ==201){
          wx.redirectTo({
            url: '/pages/detail/detail'
          })
        /*  that2.setData({
            question: '您已经回答完全部问题,得分为' + row.point,
            items: []
          }) */
        }else if(row.code == 300){
          that.setData({
            question: row.message,
            items: []
          })
        }else if(row.code == 200){
          that.setData({
            questId: row.quest.id,
            question: row.quest.question,
            items: row.answers,
            total:row.total,
            current:row.current,
            correct_point: row.quest.correct_point
          })
        }else{
          console.log('system error');
        }
        
      });

  }
})
